package org.grottarossa.ui.core.base;

import java.util.ArrayList;
import java.util.Hashtable;

import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.FormFieldLocation;
import org.grottarossa.ui.core.FormFieldProperties;
import org.grottarossa.ui.core.Notifier;
import org.grottarossa.ui.core.PopupViewer;

import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Window;

/***
 * Classe generica da cui devono ereditare tutte le form
 * @author 
 *
 */
public abstract class BaseForm extends Form implements IBaseComponent
{
	/*** Gestisce le visualizzazioni in popup ***/
	private PopupViewer m_popupViewer = new PopupViewer();

	/*** Gestisce i messaggi di notifica a video ***/
	private Notifier m_notifier;

	/*** Layout della form ***/
	protected GridLayout m_layout;

	/*** Barra dei pulsanti associata ***/
	protected com.vaadin.ui.Layout m_externalButtonsBar;

	/*** Pulsanti da aggiungere alla dispatch buttons bar ***/
	protected ArrayList<Button> m_buttonsForDispatchBar = new ArrayList<Button>();

	/*** Propriet� dei campi nella form ***/
	protected Hashtable<String, FormFieldProperties> m_fieldsProperties = new Hashtable<String, FormFieldProperties>();

	/*** Mappa dei campi ***/
	protected Hashtable<String, Field> m_fields = new Hashtable<String, Field>();

	protected final ApplicationSession m_appSession;

	public BaseForm(ApplicationSession appSession)
	{
		this.m_appSession = appSession;
		setValidationVisible(false);
		setValidationVisibleOnCommit(false);
		setWriteThrough(false);
	    setInvalidCommitted(false);
	}

	/***
	 * Inizializza i componenti grafici
	 */
	protected void initializeComponents()
	{
		//Vuoto di default perch� spesso le form non devono inizializzare nessun componente
	}

	@Override
	protected void attachField(Object propertyId, Field field)
	{
		m_fields.put((String)propertyId, field);

		String propertyName = propertyId.toString().toLowerCase();

		if(m_fieldsProperties.containsKey(propertyName))
		{
			FormFieldProperties properties = m_fieldsProperties.get(propertyName);

			m_layout.addComponent(field,
					properties.getLocation().getStartCol(),
					properties.getLocation().getStartRow(),
					properties.getLocation().getEndCol(),
					properties.getLocation().getEndRow());

			if(properties.isWidthSet())
				field.setWidth(properties.getWidthValue(), properties.getWidthUnit());

			if(properties.isHeightSet())
				field.setHeight(properties.getHeightValue(), properties.getHeightUnit());

			field.setRequired(properties.isRequired());
			field.setReadOnly(properties.isReadonly());

			if(properties.getCustomCaption() != null)
				field.setCaption(properties.getCustomCaption());
		}
	}

	/***
	 * Associa una buttons bar esterna alla form in modo da poterci caricare i bottoni
	 * @param bar
	 */
	public void setButtonLayout(com.vaadin.ui.Layout bar)
	{
		m_externalButtonsBar = bar;

		if(m_externalButtonsBar != null)
			for(Button b : m_buttonsForDispatchBar)
			{
				m_externalButtonsBar.addComponent(b);
			}
	}

	public Window showInPopup(Component view, int width, int height, String caption,
			boolean sizable, boolean modal)
	{
		return m_popupViewer.showInPopup(view, width, height, caption, sizable, modal, getWindow());
	}

	public Window showInPopup(Component view, int width, int height, String caption,
			boolean sizable, boolean modal, Window activeWindow)
	{
		return m_popupViewer.showInPopup(view, width, height, caption, sizable, modal, activeWindow);
	}

	public void removePopupFromWindow(Window popup, Window activeWindow)
	{
		m_popupViewer.removePopupFromWindow(popup, activeWindow);
	}

	public void removePopup(Window popup)
	{
		m_popupViewer.removePopupFromWindow(popup, getWindow());
	}

	public void openUrl(String url)
	{
		m_popupViewer.openUrl(url, getWindow());
	}

	public void showWarning(String message)
	{
		m_appSession.getNotifier().showWarning(message);
	}

	public void showError(String message)
	{
		m_appSession.getNotifier().showError(message);
	}

	public void showError(Exception ex)
	{
		m_appSession.getNotifier().showError(ex);
	}

	public void showInfo(String message)
	{
		m_appSession.getNotifier().showInfo(message);
	}

	public void showInDetailArea(Component component)
	{
		//Per default non fa nulla, deve essere sovrascritto se serve nelle form specifiche
	}
}
